﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Net;
using System.Collections;
using System.Xml;
using System.Reflection;
using SlideShow;
namespace SlideShow
{
    class Image_item
    {
        public Image image;
        public string file_name;
        public Image_item() { }
        public Image_item(Image image, string file_name)
        {
            this.image = image;
            this.file_name = file_name;
        }
    }

    class Schedule_item
    {
        public DateTime date;
        public ArrayList images;//<a list of image>
        public Schedule_item(DateTime date, ArrayList images)
        {
            this.date = date;
            this.images = images;
        }
        public Schedule_item() { }
    }

    class Schedule
    {
        public Schedule_item next_playlist = new Schedule_item();//<Schedule_item>

        public ArrayList DB_schedule = new ArrayList();//schedule get from DB

        public ArrayList images_db = new ArrayList();

        private Object SQL_lock = new Object();

        private string assetID;

        public Schedule(string assetID)
        {
            this.assetID = assetID;
        }
        //public ArrayList get_pic(ArrayList image_array)
        //{
        //    images_db = image_array;
        //    return image_array;
        //}

        public void get_schedule_DB()// need to be a new sub thread
        {
            //connection string
            //String constr = "Data Source=SQL01;Initial Catalog=SuperSign;User ID=sa;Password=sa";
            lock (SQL_lock)
            {
                String constr = "Data Source=DATABASESERVER;Initial Catalog=SuperSign;Persist Security Info=True;User ID=binbin;Password=gotigergo";
                SqlConnection conn = new SqlConnection(constr);
                try
                {
                    conn.Open();
                    //String sql = "select [DateTime] from dbo.ASSETSCHEDLOG where AssetID='4871' and DateTime>='2012-01-01 23:59:00' ";
                    String sql = "select [DateTime] from dbo.ASSETSCHEDLOG where AssetID='" + assetID + "' and DateTime>='" + DateTime.Now.ToLocalTime().ToString()
                                        + "' and DateTime <='" + DateTime.Now.AddMinutes(33).ToString() + "'";//should add 33 min
                    SqlDataAdapter find = new SqlDataAdapter(sql, conn);
                    DataSet save = new DataSet();
                    find.Fill(save);
                    if (save != null)
                    {
                        foreach (DataRow row in save.Tables[0].Rows)
                        {
                            //check if is already in the m_schedule
                            DateTime temp = Convert.ToDateTime(row["DateTime"]);

                            if (DB_schedule.Contains(temp) != true)//not exist in m_schedules
                            {
                                DB_schedule.Add(temp);
                                //schedules.Add(new Schedule_item(temp, images_db));
                            }
                        }
                    }

                    conn.Close();
                }
                catch (Exception e)
                {
                    //RT_status.Text = RT_status.Text + "can not connect to database , please check your network \n";
                    Console.WriteLine(e.Message);
                }
            }
            //finally
            //{
            //    if (m_schedules.Count == 0)//no schedule conntinue check the time to access database
            //        Access_DB_timer.Enabled = true;
            //    else
            //    {
            //        Check_download_timer.Enabled = true;
            //    }
            //}
        }

        public Schedule_item create_schedule(ArrayList images)
        {
            if (images != null && images.Count > 0)
            {
                //next_playlist.Clear();
                next_playlist = new Schedule_item((DateTime)DB_schedule[0], images);
            }
            return next_playlist;
        }
    }
}
